#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "modbus_tcp_slave.h"
#include "modbus_tcp_master.h"
#include "create_modbus_custom_slave.h"
#include "modbus_rtu485.h"
#include "modbus_config.h"
#include "modbus_manager.h"
#include "../debug.h"
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "../misc.h"


#define MAX_THREADS 6

void *init_modbus_manager(void *arg)
{
    Misc_InitThread(__func__);
    
    pthread_t threads[MAX_THREADS];
    int active_threads = 0;

    parse_config_file();

    const ConfigData *config_data = get_config_data_instance();

    if (strcmp(config_data->master1.enable, "true") == 0)
    {
        pthread_create(&threads[active_threads], NULL, modbus_tcp_master1_thread, NULL);
        printf("[%s][%d] Successfully created modbus_tcp_master1_thread\n", __func__, __LINE__);
        active_threads++;
    }

    if (strcmp(config_data->master2.enable, "true") == 0)
    {
        pthread_create(&threads[active_threads], NULL, modbus_tcp_master2_thread, NULL);
        printf("[%s][%d] Successfully created modbus_tcp_master2_thread\n", __func__, __LINE__);
        active_threads++;
    }

    if (strcmp(config_data->slave1.enable, "true") == 0)
    {
        pthread_create(&threads[active_threads], NULL, modbus_tcp_slave1_thread, NULL);
        printf("[%s][%d] Successfully created modbus_tcp_slave1_thread\n", __func__, __LINE__);
        active_threads++;
    }

    if (strcmp(config_data->slave2.enable, "true") == 0)
    {
        pthread_create(&threads[active_threads], NULL, modbus_tcp_slave2_thread, NULL);
        printf("[%s][%d] Successfully created modbus_tcp_slave2_thread\n", __func__, __LINE__);
        active_threads++;
    }

    if (strcmp(config_data->uart2.enable, "true") == 0)
    {
        pthread_create(&threads[active_threads], NULL, modbus_rtu_uart2_thread, NULL);
        printf("[%s][%d] Successfully created modbus_rtu_uart2_thread\n", __func__, __LINE__);
        active_threads++;
    }

    if (strcmp(config_data->uart3.enable, "true") == 0)
    {
        pthread_create(&threads[active_threads], NULL, modbus_rtu_uart3_thread, NULL);
        printf("[%s][%d] Successfully created modbus_rtu_uart3_thread\n", __func__, __LINE__);
        active_threads++;
    }

    if (strcmp(config_data->slave1.enable, "true") == 0)
    {
        pthread_create(&threads[active_threads], NULL, customModbus_server1_thread, NULL);
        //printf("[%s][%d] Successfully created customModbus_server1_thread\n", __func__, __LINE__);
        active_threads++;
    }

    if (strcmp(config_data->slave1.enable, "true") == 0)
    {
        pthread_create(&threads[active_threads], NULL, customModbus_server2_thread, NULL);
        //printf("[%s][%d] Successfully created customModbus_server2_thread\n", __func__, __LINE__);
        active_threads++;
    }

    for (int i = 0; i < active_threads; i++)
    {
        pthread_join(threads[i], NULL);
    }

    return 0;
}
